﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace BuildRunner
{
	public class VC6
	{
		public CompilerType Type { get; set; }
		public List<string> Setting { get; set; }
		public string FilePath { get; set; }

		public static List<VC6> Analyse( string strFilePath )
		{
			List<VC6> ltRet = new List<VC6>();
			string strText = File.ReadAllText( strFilePath, Encoding.GetEncoding( "Shift_JIS" ) );
			Regex reg = new Regex( "Project: \"(?<name>.*?)\"=\"(?<filename>.*?)\" - Package Owner=<4>" );
			var match = reg.Matches( strText );
			foreach( Match item in match ) {
				string strName = item.Groups["name"].Value;
				string strFileName = item.Groups["filename"].Value;
				ltRet.Add( AnalyseProject( strFilePath, strFileName ));
			}

			return ltRet;
		}

		public static VC6 AnalyseProject( string strFilePath, string strProjectFileName )
		{
			string strText = File.ReadAllText( Path.GetDirectoryName( strFilePath ) + "\\" + strProjectFileName
											, Encoding.GetEncoding( "Shift_JIS" ) );
			VC6 vc6 = new VC6()
			{
				FilePath = Path.GetDirectoryName( strFilePath ) + "\\" + strProjectFileName,
				Type = CompilerType.VC6,
			};

			//!IF  "$(CFG)" == "Gpha - Win32 Release"
			//!ELSEIF  "$(CFG)" == "Gpha - Win32 Debug"
			Regex reg = new Regex( "^!(IF|ELSEIF)  \"\\$\\(CFG\\)\" == \"(?<name>.*?)\"", RegexOptions.Multiline );
			var match = reg.Matches( strText );
			vc6.Setting = new List<string>();
			foreach( Match item in match ) {
				string strName = item.Groups["name"].Value;
				vc6.Setting.Add( strName );
			}

			return vc6;
		}
	}
}
